import type { Ref } from "vue";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { $t } from "@/plugins/i18n";

interface UseDeptFormDialog {
    /** 对话框状态 */
    dialogVisible: Ref<boolean>;
    /** 对话框模式: 'create' 或 'edit' */
    dialogMode: Ref<"create" | "edit">;
    /** 打开新增对话框 */
    openAddDialog: (hasParent: boolean) => void;
    /** 打开编辑对话框 */
    openEditDialog: (id?: string) => void;
}

export function useDeptFormDialog(): UseDeptFormDialog {
    /** 对话框状态 */
    const dialogVisible = ref(false);
    /** 对话框模式: 'create' 或 'edit' */
    const dialogMode = ref<"create" | "edit">("create");

    /** 打开新增对话框 */
    function openAddDialog(hasParent: boolean): void {
        if (!hasParent) {
            ElMessage.warning($t("placeholder.selectParentDepartment"));
            return;
        }
        dialogMode.value = "create";
        dialogVisible.value = true;
    }

    /** 打开编辑对话框 */
    function openEditDialog(id?: string): void {
        if (!id) {
            ElMessage.warning($t("placeholder.selectEditDepartment"));
            return;
        }
        dialogMode.value = "edit";
        dialogVisible.value = true;
    }

    return {
        dialogVisible,
        dialogMode,
        openAddDialog,
        openEditDialog,
    };
}
